<?php
class Administration_PlayersController extends Ehl_Controller_Admin_Action
{

    public function indexAction()
    {
        $identite = Zend_Auth::getInstance()->getIdentity();
        $options = array();
        $form = new Ehl_Form_Recherche($options, 'adminplayers');
        $objetModel = new Administration_Model_Players();
        $request = $this->getRequest();
        $form->populate($request->getParams());
        if ($request->isPost()) {
            $status = $form->isValidPartial($request->getPost());
        }
        $options['tout'] = (bool) $this->_getParam('tout', FALSE);
        $select = $objetModel->getListSelect($form->getValues(), "", $identite, $options);
        $form->setAction($this->view->smartUrl(null, null, null, array(
            'page' => (int) $this->_getParam('page', 1), 
            'tout' => $options['tout']
        )));
        $form->getElement('SelectionMesTous')->setValue($options['tout']);
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'id' => __('id'), 
            'lastname' => __('lastname'), 
            'firstname' => __('firstname'), 
            'nickname' => __('nickname'), 
            'number' => __('number'), 
            'height' => __('height'), 
            'weight' => __('weight'), 
            'shoots' => __('shoots'), 
            'born' => __('born'), 
            'creationUser' => array(
                'name' => __('created by'), 
                'visible' => $options['tout'], 
                'search' => $options['tout']
            ), 
            'creationDate' => __('création')
        );
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ), 
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
        $this->view->form = $form;
    }

    public function editerAction()
    {
        $request = $this->getRequest();
        
        $teams = new Administration_Model_Teams();
        $liste = $teams->listerPourSelect("name", true);
        
        $form = new Administration_Form_Players($liste);
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                $model = new Administration_Model_Players($form->getValues());
                $model->sauver();
                return $this->_helper->redirector('index');
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id != 0) {
                $model = new Administration_Model_Players();
                $model->chercher($id);
                $form->populate($model->toArray());
            }
        }
        $this->view->form = $form;
    }

    public function voirAction()
    {
        
        $request = $this->getRequest();
        if ($this->getRequest()->isGet()) {
            $id = $this->_getParam('id', 0);
            $model = new Administration_Model_Players($id);
            
            $teams = new Administration_Model_Teams();
            $liste = $teams->listerPourSelect("name", true);
            
            $form = new Administration_Form_Players($liste, "Voir");
            $form->populate($model->toArray());
            $form->getElement("pictures")->setImage("/images/players/pictures/" . $model->getPictures());
            $this->view->form = $form;
            $this->view->model = $model;
        }
    }

    /* (non-PHPdoc)
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback( $row, $view )
    {
        if (isset($row['born']) && $row['born'] != "") {
            $row['born'] = date(substr($row['born'], 0, strpos($row['born'], " ")));
        }
    }

}

